//
//  VideoViewCell.m
//  Product_NEWS
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 FCH. All rights reserved.
//

#define padding 10 * SCREEN_WIDTH
#import "VideoViewCell.h"
#import "UIImageView+WebCache.h"

@interface VideoViewCell ()
@property (nonatomic , retain) titleLabel *titleLabel;
@property (nonatomic , retain) WordTite *descLabel;
@property (nonatomic , retain) UIImageView *imageview;
@property (nonatomic , retain) UIImageView *playcoverImage;

@property (nonatomic , retain) UIImageView *lengthImage;
@property (nonatomic , retain) WordTite *lengthLabel;
@property (nonatomic , retain) UIImageView *playImage;
@property (nonatomic , retain) WordTite *playcountLabel;
@property (nonatomic , retain) UIImageView *reply;
@property(nonatomic,assign)NSInteger replyCount;
@property(nonatomic,retain)UIView *view;
@property (nonatomic , retain) UIView *lineV;
@property(nonatomic,retain)UIImageView *common;
@property(nonatomic,retain)WordTite *commonLabel;
@property(nonatomic,retain)UIImageView *back;

@property(nonatomic,retain)UIButton *playBtn;


@end



@implementation VideoViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"cell";
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    VideoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[VideoViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        
//图片
        self.imageview = [[UIImageView alloc]init];
        [self.contentView addSubview:self.imageview];
        [self.contentView addSubview:self.imageview];
        self.imageview.frame = CGRectMake(0, 0, 375 * SCREEN_WIDTH,375 * SCREEN_WIDTH * 0.5);
        self.imageView.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        [_imageview release];
        
        
        
 //播放器
//        self.contentView.backgroundColor = [UIColor clearColor];
//        
//        self.moviePlayer = [[MPMoviePlayerController alloc] init];
//        [[self.moviePlayer view] setFrame:self.imageview.bounds];
//        [self addSubview:[self.moviePlayer view]];
//        
//        
//        
        
        UIImageView *playcoverImage = [[UIImageView alloc]init];
        [self.contentView addSubview:playcoverImage];
        self.playcoverImage = playcoverImage;
        self.playcoverImage.frame = CGRectMake( 375 * SCREEN_WIDTH / 2 - 30 * SCREEN_WIDTH ,375 * SCREEN_WIDTH *0.5 / 2,45 * SCREEN_WIDTH, 45 * SCREEN_HEIGHT);
        self.playcoverImage.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.playcoverImage];
        [_playcoverImage release];
   
//题目
        self.titleLabel = [[titleLabel alloc]init];
        self. titleLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:self.titleLabel];
        self.titleLabel.frame = CGRectMake(padding, self.imageview.frame.size.height + 2 * padding, 375 * SCREEN_WIDTH, 20  * SCREEN_HEIGHT);
        
         self.titleLabel.font = [UIFont systemFontOfSize:17*SCREEN_WIDTH];
        
//描述
                WordTite *descLabel = [[WordTite alloc]init];
                descLabel.textAlignment = NSTextAlignmentLeft;
                [self.contentView addSubview:descLabel];
                self.descLabel = descLabel;
        self.descLabel.frame = CGRectMake(padding, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 5 * SCREEN_HEIGHT, SCREEN_WIDTH * 375, 20 * SCREEN_HEIGHT);
        self.descLabel.font = [UIFont systemFontOfSize:13*SCREEN_WIDTH];
      
     
        
        
   
        
        //灰线
        UIView *lineV = [[UIView alloc]init];
        lineV.backgroundColor = [UIColor colorWithRed:0.251 green:0.865 blue:0.115 alpha:1.000];
        [self.contentView addSubview:lineV];
        self.lineV = lineV;
        [self.contentView addSubview:lineV];
        self.lineV.frame = CGRectMake(padding, self.descLabel.frame.origin.y + self.descLabel.frame.size.height + 2* SCREEN_HEIGHT,355 * SCREEN_WIDTH , SCREEN_HEIGHT);
      
        self.lineV.backgroundColor = [UIColor colorWithRed:0.780 green:0.800 blue:0.769 alpha:1.000];
//时长图标
        UIImageView *lengthImage = [[UIImageView alloc]init];
        [self.contentView addSubview:lengthImage];
        lengthImage.frame = CGRectMake(padding, self.lineV.frame.origin.y + SCREEN_HEIGHT + 10, 20 * SCREEN_WIDTH, 20 * SCREEN_HEIGHT);
        lengthImage.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        self.lengthImage = lengthImage;
        [self.contentView addSubview:lengthImage];
        
//时长
        WordTite *lengthLabel = [[WordTite alloc]init];
       
        lengthLabel.font = [UIFont systemFontOfSize:15*SCREEN_WIDTH];
        lengthLabel.frame = CGRectMake(padding + self.lengthImage.frame.size.width + 5* SCREEN_WIDTH, self.lengthImage.frame.origin.y , 50 * SCREEN_WIDTH, 20 *SCREEN_HEIGHT);
               [self.contentView addSubview:lengthLabel];
        self.lengthLabel = lengthLabel;
        
//播放按钮
        UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        playBtn.frame = self.playcoverImage.frame;
        playBtn.backgroundColor = [UIColor clearColor];
        UIImage *buttonImage = [UIImage imageNamed:@"iconfont-iconfontcolor95"];
        [playBtn setImage:buttonImage forState:UIControlStateNormal];
        [self.contentView addSubview:playBtn];
        playBtn.backgroundColor = [UIColor clearColor];
        [playBtn addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
       self.playBtn = playBtn;
        
        
        
//播放图标
        UIImageView *playImage = [[UIImageView alloc]init];
        playImage.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        playImage.frame = CGRectMake(self.lengthLabel.frame.origin.x + self.lengthLabel.frame.size.width + 20 * SCREEN_WIDTH, self.lengthLabel.frame.origin.y, 20 * SCREEN_WIDTH, 20 * SCREEN_HEIGHT);
        [self.contentView addSubview:playImage];
         self.playImage = playImage;
        
//播放数
        WordTite *playcountLabel = [[WordTite alloc]init];
        playcountLabel.textColor = [NightModeHandle shareNightModeHandle].wordColor;
        playcountLabel.font = [UIFont systemFontOfSize:15*SCREEN_WIDTH];
        [self.contentView addSubview:playcountLabel];
        playcountLabel.frame = CGRectMake(self.playImage.frame.origin.x + self.playImage.frame.size.width + 5 * SCREEN_WIDTH, self.playImage.frame.origin.y, 60 * SCREEN_WIDTH, 20 * SCREEN_HEIGHT);
        self.playcountLabel = playcountLabel;
        
        
  //分享
        
        self.common = [[UIImageView alloc]initWithFrame:CGRectMake(self.playImage.frame.origin.x + self.playImage.frame.size.width + 100 * SCREEN_WIDTH, self.playImage.frame.origin.y , 20 *SCREEN_WIDTH, 20 * SCREEN_HEIGHT)];
         playImage.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        playcountLabel.textColor = [NightModeHandle shareNightModeHandle].wordColor;
        [self.common setImage:[UIImage imageNamed:@"common"]];
    
         [self.contentView addSubview:self.common];
 
        WordTite *commonLabel = [[WordTite alloc]init];
        commonLabel.font = [UIFont systemFontOfSize:15*SCREEN_WIDTH];
        [self.contentView addSubview:commonLabel];
        commonLabel.frame = CGRectMake(self.playImage.frame.origin.x + self.playImage.frame.size.width + 130 * SCREEN_WIDTH, self.playImage.frame.origin.y - 1* SCREEN_HEIGHT, 80* SCREEN_WIDTH, 20 * SCREEN_HEIGHT);
        
        self.commonLabel = commonLabel;
        
        [_commonLabel release];
        
        
        UIButton *share = [UIButton buttonWithType:UIButtonTypeCustom];
        [share addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
     share.frame =CGRectMake(self.playImage.frame.origin.x + self.playImage.frame.size.width + 200 * SCREEN_WIDTH, self.playImage.frame.origin.y - 2 * SCREEN_HEIGHT, 20 * SCREEN_HEIGHT, 20 * SCREEN_HEIGHT);
        share.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        [share setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        [self.contentView addSubview:share];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor clearColor];
        [btn addTarget:self action:@selector(commonAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = self.commonLabel.frame;
        [self.contentView addSubview:btn];
        
//  间距
        self.view = [[UIView alloc]initWithFrame:CGRectMake(0, self.playcountLabel.frame.origin.y + 30 * SCREEN_HEIGHT, 375 * SCREEN_WIDTH, 20)];
        self.view.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        [self.contentView addSubview:self.view];
  
        
        
        self.back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 355 * SCREEN_WIDTH, self.playcountLabel.frame.origin.y + 30 * SCREEN_HEIGHT)];
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        self.back.layer.borderColor = CGColorCreate(colorSpace,(CGFloat[]){ 188/255.0, 193/255.0, 183/255.0, 1 });
        [self.back.layer setMasksToBounds:YES];
        self.back.layer.cornerRadius=2;
[self.back.layer setBorderWidth:1.0];
        
        [self.contentView addSubview:self.back];
       
        
        
//        
//         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
        
    }
    return self;
}


-(void)setVideoData:(VideoData *)videoData
{
    if (_videoData != videoData) {
        [_videoData release];
        _videoData  = [videoData retain];
    }
    NSURL *url =[NSURL URLWithString:self.videoData.cover];
    [self.imageview sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"background"]];

    self.titleLabel.text = self.videoData.title;
    self.descLabel.text = self.videoData.Description;
    self.lengthImage.image = [UIImage imageNamed:@"iconfont-time"];
     self.lengthLabel.text = [self convertTime:videoData.length];
    self.playImage.image = [UIImage imageNamed:@"iconfont-bofang"];
    self.playcountLabel.text =[self changePeople:videoData.playCount];
    
    [NSString stringWithFormat:@"%ld", (long)self.videoData.playCount];

     self.common.image = [UIImage imageNamed:@"common"];
    self.commonLabel.text = [self changePeople:videoData.replyCount ];
 
}


//-(void)playbackDidFinish
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//    self.playBtn .hidden = NO;
//}
//




-(void)playVideo:(UIButton *)btn
{
  
    
    
    [self.myDelegate videoPlay:self.indexPath];
    
}

-(void)shareAction:(UIButton *)btn {
    
    
    [self.myDelegate shareVideo:self.
     
     indexPath];
}


-(void)commonAction:(UIButton *)btn {
    
    
    [self.myDelegate  VideoCommon :self.
     
     indexPath];
}








@end
